<?php


namespace app\crm\controller\clues;

use app\common\controller\CrmController;
use app\crm\model\CrmClues;
use app\crm\traits\Curd;

class Record extends CrmController
{
    protected $validate = 'customer\Record';
    protected $model = 'CrmRecord';

    use Curd;

    public function initialize()
    {
        parent::initialize();
    }

     /**
     * @NodeAnotation(title="列表")
     */    
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            $count = $this->model::with('createUser')
                ->where($where)
                ->count();
            $list = $this->model::with('createUser')
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }

    /**
     * 添加跟进
     */
    public function add($cid,$type)
    {
        $customer = CrmClues::get($cid);
        if (empty($customer)) {
            return $this->error('无效的请求id');
        }
        $post = $this->request->post();
        $post['types'] = $type;
        $post['types_id'] = $cid;
        $validate = new $this->validate();
        if ($validate->check($post) == false)
            return $this->error($validate->getError());

        $result = $this->model->allowField(true)->save($post);
        if( !$result ){
            return $this->error('保存失败');
        }
        return $this->success('保存成功');
    }

}